package com.hsck.order.kds.base.isnoam.view

import android.app.Activity
import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.ViewStub
import android.widget.FrameLayout
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.viewbinding.ViewBinding
import com.hsck.order.kds.R

abstract class BaseFragment<VB : ViewBinding> : Fragment() {

    var _binding: VB? = null
    protected val binding: VB
        get() = _binding!!

    /**
     * 依附的Activity
     */
    var activity: Activity? = null

    /**
     * 是否已经加载过数据
     */
    private var mHasLoadedData = false

    /**
     * Fragment中由于服务器或网络异常导致加载失败显示的布局。
     */
    private var loadErrorView: View? = null
    private var noDataView: View? = null

    /**
     * Fragment中inflate出来的布局。
     */
    protected var rootView: View? = null
    protected var mIsVisible = false


    override fun onAttach(context: Context) {
        super.onAttach(context)
        // 缓存当前依附的activity
        activity = requireActivity()
    }

    override fun onResume() {
        super.onResume()
        //当Fragment在屏幕上可见并且没有加载过数据时调用
        if (!mHasLoadedData) {
            registerObserver(mIsVisible)
            loadDataOnce()
            mHasLoadedData = true
        }
    }

    override fun setUserVisibleHint(isVisibleToUser: Boolean) {
        mIsVisible = isVisibleToUser
    }

    /**
     * 页面首次可见时调用一次该方法，在这里可以请求网络数据等。
     */
    open fun loadDataOnce() {}

    open fun registerObserver(mIsVisible: Boolean) {}

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        _binding = inflateBinding(inflater, container)

        // Inflate fragment_base.xml 作为根视图
        rootView = inflater.inflate(R.layout.fragment_base, container, false) as ViewGroup

        // 将子布局添加到根视图中
        val contentFrame = rootView!!.findViewById<FrameLayout>(R.id.content)
        contentFrame.addView(binding.root)

        return rootView
    }


    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }

    // 抽象方法，由子类实现来提供具体的 ViewBinding 实例
    protected abstract fun inflateBinding(inflater: LayoutInflater, container: ViewGroup?): VB


    /**
     * 当Fragment中的加载内容服务器返回失败或网络异常，通过此方法显示提示界面给用户。
     *
     * @param tip 界面中的提示信息
     * @param block 点击屏幕重新加载，回调处理。
     */
    protected fun showLoadErrorView(tip: String = "网络异常", block: View.() -> Unit) {
        if (loadErrorView != null) {
            loadErrorView?.visibility = View.VISIBLE
            return
        }
        if (rootView != null) {
            val viewStub = rootView!!.findViewById<ViewStub>(R.id.loadErrorView)
            if (viewStub != null) {
                loadErrorView = viewStub.inflate()
                val loadErrorText = loadErrorView?.findViewById<TextView>(R.id.loadErrorText)
                if (tip.isNotEmpty()) {
                    loadErrorText?.text = tip
                }
                val loadErrorkRootView = loadErrorView?.findViewById<View>(R.id.loadErrorkRootView)
                loadErrorkRootView?.setOnClickListener {
                    it.block()
                }
            }
        }
    }

    protected fun showNoDataView(tip: String = "暂无订单", block: View.() -> Unit) {
        if (noDataView != null) {
            noDataView?.visibility = View.VISIBLE
            return
        }
        if (rootView != null) {
            val viewStub = rootView!!.findViewById<ViewStub>(R.id.loadNoDataView)
            if (viewStub != null) {
                noDataView = viewStub.inflate()
                val loadErrorText = noDataView?.findViewById<TextView>(R.id.loadNoDataText)
                if (tip.isNotEmpty()) {
                    loadErrorText?.text = tip
                }
                val loadNoDataRootView = noDataView?.findViewById<View>(R.id.loadNoDataRootView)
                loadNoDataRootView?.setOnClickListener {
                    it.block()
                }
            }
        }
    }

    protected fun hideLoadErrorView() {
        loadErrorView?.visibility = View.GONE
    }

    protected fun hideNoDataView() {
        noDataView?.visibility = View.GONE
    }

}
